package com.codeckz.library_face

import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.os.Message
import androidx.annotation.RequiresApi
import androidx.camera.core.CameraSelector
import com.codeckz.library_face.controller.AndroidFilterFace
import com.codeckz.library_face.controller.BaseFilterFace
import com.codeckz.library_face.controller.ICameraController
import com.codeckz.library_face.ui.FaceGetter19Activity
import com.codeckz.library_face.ui.FaceGetterActivity
import java.io.Serializable

/**
 *@packageName com.codeckz.library_face
 *@author kzcai
 *@date 2022/1/11
 */
object FaceGetterPlugins {

    private val handler = object : Handler(Looper.myLooper()!!) {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            if (msg.what==1){
                val data = msg.obj as ByteArray
                faceListener?.invoke(data)
            }
        }
    }

    fun sendImageData(data: ByteArray){
        val msg = Message()
        msg.what = 1
        msg.obj = data
        handler.handleMessage(msg)
    }

    private var faceListener: ((data: ByteArray) -> Unit)?=null

    fun startFace(
        context: Context,
        listener: (data: ByteArray) -> Unit,
        options:FaceGetOptions = FaceGetOptions()
    ) {
        faceListener = listener
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            FaceGetterActivity.openFacePage(context,options)
        }else{
            FaceGetter19Activity.openFacePage(context,options)
        }

    }
}

class FaceGetOptions:Serializable{
    var checkNum = 2
    var cameraFaceing = ICameraController.BACK_CAMERA
    var isUVC:Boolean = false
    var faceFilter:BaseFilterFace = AndroidFilterFace()
    var productId = 0
    var vendorId = 0
}